package hk.m4s.lr.repair.test.ui.baoyang;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.graphics.drawable.RoundedBitmapDrawable;
import android.support.v4.graphics.drawable.RoundedBitmapDrawableFactory;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.amap.api.maps.Projection;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.target.BitmapImageViewTarget;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import framework.common.Constant;
import framework.common.alertview.AlertView;
import framework.common.alertview.OnItemClickListener;
import framework.common.baseui.UeBaseActivity;
import framework.common.zanetwork.core.ResponseCallback;
import framework.utils.SharedPreferencesUtils;
import hk.m4s.lr.repair.test.R;
import hk.m4s.lr.repair.test.model.MaintenModelResult;
import hk.m4s.lr.repair.test.model.MaintenancekModel;
import hk.m4s.lr.repair.test.model.UserinfoModel;
import hk.m4s.lr.repair.test.model.projectModle;
import hk.m4s.lr.repair.test.service.device.DeviceListervice;
import hk.m4s.lr.repair.test.service.notice.NoticeService;
import hk.m4s.lr.repair.test.service.user.MyService;
import hk.m4s.lr.repair.test.ui.adapter.RepairPartslAdapter;
import hk.m4s.lr.repair.test.ui.adapter.RepairWeiPartslAdapter;
import hk.m4s.lr.repair.test.ui.equipment.SqcanDeviceWeixiuActivity;
import hk.m4s.lr.repair.test.utils.ToastUtil;
import hk.m4s.lr.repair.test.views.CornersTransform;
import hk.m4s.lr.repair.test.views.InnerGridView;
import okhttp3.Response;

/**
 * Created by qi.wang on 2017/3/27.
 * 扫一扫设备保养
 */

public class SqcanDeviceBaoActivity extends UeBaseActivity implements OnItemClickListener {
    private Context context;
    private ImageView equiment_img;
    private TextView equ_number,ok_send_order;
    private String decice_id = "";
    private String state = "";
    private InnerGridView innerGridView;
    private List<MaintenancekModel.ProjectBean> projectList = new ArrayList<MaintenancekModel.ProjectBean>();
    private List<MaintenancekModel.ProjectBean> selectList = new ArrayList<MaintenancekModel.ProjectBean>();
    RepairPartslAdapter adapter;
    LinearLayout tipsLay,equiment_lay;
    ScrollView home_send_order;
    private String ids;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        baseSetContentView(R.layout.activity_sqcan_equipment);
        context = this;
        hiddenNewMessage();
        hiddenFooter();
        showGoBackBtn();
        equiment_lay =(LinearLayout) findViewById(R.id.equiment_lay);
        tipsLay=(LinearLayout)findViewById(R.id.tipsLay);
        equiment_img = (ImageView) findViewById(R.id.equiment_img);
        equ_number = (TextView) findViewById(R.id.equ_number);
        ok_send_order= (TextView) findViewById(R.id.ok_send_order);
        innerGridView = (InnerGridView) findViewById(R.id.innergrid);
        decice_id = getIntent().getStringExtra("decice_id");
        state = getIntent().getStringExtra("deciceState");

        setTitleText("设备保养");
        getMaintenancek();

    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ok_send_order:
                if (adapter != null) {
                    if (adapter.items.size() > 0) {

                        for (MaintenancekModel.ProjectBean modle : adapter.items) {
                            if (modle.getState().equals("1")) {
                                selectList.add(modle);
                            }
                        }
                    }
                }
                if (selectList.size() > 0) {
                    new AlertView("提示", "您真的确定保养这些项目吗？确认后将不能修改和删除", "取消", new String[]{"确定"}, null, this, AlertView.Style.Alert, this).setCancelable(true).show();
                } else {
                    ToastUtil.toast(context, "请选择保养项");
                }
                break;
        }

    }

    @Override
    public void onItemClick(Object o, int position) {
        //判断是否是拓展窗口View，而且点击的是非取消按钮
        if (position == -1) {
            return;
        } else {
            userSetUpdate();
            finish();
        }
    }

    public void getMaintenancek() {
        Map<String, Object> parmas = new HashMap<>();
        parmas.put("device_type", Constant.device_type);
        parmas.put("token", SharedPreferencesUtils.getSharedPreferences(Constant.token, ""));
        try {
            JSONObject jsondata = new JSONObject();
            jsondata.put("deviceId", decice_id);
            parmas.put("jsonText", jsondata.toString());
        } catch (JSONException e) {
        }

        DeviceListervice.maintenancekByDeviceId(context, parmas, new ResponseCallback<MaintenancekModel>() {
            @Override
            public void onSuccess(MaintenancekModel model) {
                projectList.clear();
                if (!(model == null || model.equals(""))) {
                    ids=model.getId();
                    if(model.getDeviceId()!=null) {
                        tipsLay.setVisibility(View.GONE);
                        ok_send_order.setVisibility(View.VISIBLE);
                        equiment_lay.setVisibility(View.VISIBLE);
                        if (!(model.getDeviceId() == null || model.getDeviceId().equals(""))) {
                            equ_number.setText("设备编号：" + model.getDeviceId());
                        }
                        try {
                            Glide.with(context).load(model.getDeviceLogo()).asBitmap()
                                    .error(R.mipmap.device_list_no_img)
                                    .into(equiment_img);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        try {
                            if (!(model.getProject() == null || model.getProject().equals(""))) {
                                if (model.getProject() != null) {
                                    projectList.addAll(model.getProject());
                                    adapter = new RepairPartslAdapter(SqcanDeviceBaoActivity.this,
                                            projectList);
                                    innerGridView.setAdapter(adapter);
                                }
                            }
                        } catch (Exception e) {

                        }
                    }else{
                        tipsLay.setVisibility(View.VISIBLE);
                        ok_send_order.setVisibility(View.GONE);
                        equiment_lay.setVisibility(View.GONE);

                    }
                }else{
                    tipsLay.setVisibility(View.VISIBLE);
                    ok_send_order.setVisibility(View.GONE);
                    equiment_lay.setVisibility(View.GONE);
                }

            }

            @Override
            public void onError(Response response, int errorCode, String errorMsg, Exception e) {

            }
        });
    }

    public void userSetUpdate() {
        try {
            Map<String, Object> parmas = new HashMap<>();
            parmas.put("device_type", Constant.device_type);
            parmas.put("token", SharedPreferencesUtils.getSharedPreferences(Constant.token, ""));
            JSONObject jsondata = new JSONObject();
            JSONArray array = new JSONArray();
            for (MaintenancekModel.ProjectBean modle : selectList) {
                if (modle.getState().equals("1")) {
                    array.put(new JSONObject().put("name", modle.getName()).put("state", modle.getState()));
                }
            }
            jsondata.put("id",ids);
            jsondata.put("project", array);
            parmas.put("jsonText", jsondata.toString());
            DeviceListervice.updateMaintenancekByDeviceId(context, parmas, new ResponseCallback<Map>() {
                @Override
                public void onSuccess(Map deviceModelResult) {
                    getMaintenancek();
                }

                @Override
                public void onError(Response response, int errorCode, String errorMsg, Exception e) {

                }
            });
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}
